/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package assignment_cp3120.ucm;

import assignment_cp3120.administrator.AdminView;
import assignment_cp3120.auth.Authenticate;
import assignment_cp3120.login.LoginModel;
import assignment_cp3120.login.LoginView;
import assignment_cp3120.manager.ManagerView;
import assignment_cp3120.startup.StartupView;
import assignment_cp3120.user.UserView;

/**
 * 
 *  LOGIN USE-CASE
 * 
 * Precondition: Main screen is displayed
 *  1. User clicks login on main screen
 *  2. System shows login screen
 *  3. User enters username and password
 *  4. User clicks login
 *  5. System validates user
 *  6. System logs User in
 *  7. System hides login screen
 *  8. System shows user screen 
 *<P>
 *   5.a.1 User is not valid
 *   5.a.2 System shows error dialog
 *   5.a.3 System clears username and password fields 
 *  Postcondition: Display is updated accordingly 
 * @author Richard Irons
 */
public class UCLoginUser implements UseCaseController {
    private final LoginView view;
    private final LoginModel model;

    /**
     * login user constructor, sets the view, model and frame to the input values
     * @param view
     * @param model
     * @param mainView
     */
    public UCLoginUser(LoginView view, LoginModel model){
        this.view = view;
        this.model = model;
        
        //*  2. System shows login screen
        view.showGUI();
    }

    /**
     * Starts the use case and shows the GUI
     */
    public void run(){
    //4. User clicks login         
        model.setUsername(view.getUsernameField());
        model.setPassword(new String(view.getPasswordField()));
        model.setUserType(view.getUserType());
        //5. System validates user
        System.out.println(model.getUserType());
        if(Authenticate.authUser(model.getUsername(), model.getPassword(), model.getUserType())){
            //6. System logs User in
            Authenticate.login(model.getUsername());
            //7. System hides login screen
            view.hideGUI();
            view.successMessageBox();
            if(Authenticate.getInstance().isUser()){
                StartupView.getInstance().setContents(UserView.getInstance());
                //8. System shows user screen
            }else if(Authenticate.getInstance().isManager()){
                StartupView.getInstance().setContents(new ManagerView());
            }
            else if(Authenticate.getInstance().isAdmin()){
                StartupView.getInstance().setContents(AdminView.getInstance());                
            }
        }//5.1 User is not valid
        else{                                
            //5.2 System shows error dialog
            view.errorMessageBox();
            //5.3 System clears username and password fields 
            view.clearUsernamePasswordField();
        }
    }
}